package com.echovin.esb.gateway.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.echovin.esb.gateway.mapper.GatewayRouteMapper;
import com.echovin.esb.gateway.model.GatewayServer;
import com.echovin.esb.gateway.service.GatewayRouteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.route.RouteDefinition;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

@Service
public class GatewayRouteServiceImpl implements GatewayRouteService {
    @Autowired
    GatewayRouteMapper gatewayRouteMapper;

    public GatewayRouteServiceImpl() {
    }

    @Override
    public List<RouteDefinition> getRoutes() {
        List<RouteDefinition> result = new ArrayList<>();
        List<String> list = gatewayRouteMapper.getRoutes();
        Iterator<String> iterator = list.iterator();
        while (iterator.hasNext()) {
            String content = iterator.next();
            RouteDefinition routeDefinition = JSONObject.parseObject(content, RouteDefinition.class);
            if (routeDefinition != null) {
                result.add(routeDefinition);
            }
        }
        return result;
    }

    @Override
    public int delete(GatewayServer record) {
        return gatewayRouteMapper.delete(record);
    }

    @Override
    public int update(GatewayServer record) {
        return gatewayRouteMapper.update(record);
    }
}
